package org.sacgtug.android;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class MyRemoteService extends Service {

	// 5 seconds
	private static final int LOCATION_MIN_TIME = 5000;

	// 10 meters
	private static final int LOCATION_MIN_DISTANCE = 0;

	// Location
	private static final String LONG = "LONG";
	private static final String LATI = "LATI";

	private LocationManager locationManager;

	private Location currentLocation;

	
	@Override
	public IBinder onBind(Intent intent) {
		
		if (MyService.class.getName().equals(intent.getAction())) {
            return myService;
        }
        return null;
	}
	
	private final MyService.Stub myService = new MyService.Stub() {
		
		public String getLocation() {
			return getLocationAsJSON(currentLocation);
		}
	};

	@Override
	public void onCreate() {
		super.onCreate();

		// Setting up the location manager
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		String provider = locationManager.getBestProvider(getLocationCriteria(), true);
		Log.i("## S ##", "provider = " + provider);
		locationManager.requestLocationUpdates(provider, LOCATION_MIN_TIME, LOCATION_MIN_DISTANCE, getLocationListener());
		currentLocation = locationManager.getLastKnownLocation(provider);
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	
	private LocationListener getLocationListener() {

		return new LocationListener() {

			public void onLocationChanged(Location location) {
				Log.i("## S ##", "LocationListener.onLocationChanged()");
				currentLocation = location;
			}

			public void onProviderDisabled(String arg0) {
				Log.i("## S ##", "LocationListener.onProviderDisabled()");
			}

			public void onProviderEnabled(String arg0) {
				Log.i("## S ##", "LocationListener.onProviderEnabled()");
			}

			public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
				Log.i("## S ##", "LocationListener.onStatusChanged() provider = " + arg0 + " status = " + arg1);
			}
		};
	}
	
	private Criteria getLocationCriteria() {

		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		criteria.setAltitudeRequired(false);
		criteria.setBearingRequired(false);
		criteria.setCostAllowed(false);
		criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
		criteria.setSpeedRequired(false);

		return criteria;
	}
	
	private String getLocationAsJSON(Location location) {

		if (null == location) {
			return "{\"" + LATI + "\":\"-\", \"" + LONG + "\":\"-\"}";
		}

		Map<String, String> locationData = new HashMap<String, String>();
		locationData.put(LONG, String.valueOf(location.getLongitude()));
		locationData.put(LATI, String.valueOf(location.getLatitude()));

		return new JSONObject(locationData).toString();
	}

}
